$(function () {
    $(".target.submit").on("tap", function (e) {
        var $this = $(this);
        var formvalue = [];
        var length = 0;
        $this.parents('.ui-block').find('.element').each(function () {
            length++;
            $(this).serializeForm($(this).data('id'), function (data) {
                formvalue.push(data);
            });
        });

        if (formvalue.length == length) {
            Toast.showWait();
            $.ajax({
                type: "GET",
                url: baseurl + "campaign-survey",
                timeout:5000,
                data: {
                    formvalue: JSON.stringify(formvalue),
                    mac: mac,
                    nasid: nasid,
                    name_device: name_device,
                    id_campaign: id_campaign,
                    landing_page: landing_page,
                    id_place: id_place,
                    type_time_session: type_time_session
                },
                success: function (data) {
                    if (data == 200) {
                        Toast.showMessage({typeMessage: 'success'});
                        setTimeout(function () {
                            Toast.close();
                            Toast.showWait({text: "Truy cập internet...", width: "150px"});
                            location.href = authentication_link;
                        }, 1000);
                    } else {
                        Toast.close();
                        Toast.show({text: "Xảy ra lỗi, vui lòng thử lại."});
                    }
                },
                error: function () {
                    Toast.close();
                    Toast.show({text: "Xảy ra lỗi, vui lòng thử lại."});
                }
            });
        } else {
            Toast.show({text: "Vui lòng điền đầy đủ thông tin."});
        }
        e.preventDefault();
    });


    $.fn.serializeForm = function (element, success) {
        var $this = $(this);
        switch (element) {
            case "input":
                var data = {
                    label: $this.find('.ui-label').text(),
                    value: $this.find('input').val(),
                    fieldtype: "input"
                };
                if ($.trim(data.value) == "") {
                    return;
                }
                success(data);
                break;
            case"checkboxlist":
                var value = [];
                $this.find('ul li input:checked').each(function () {
                    value.push($(this).next().text());
                });

                var data = {
                    label: $this.find('.ui-label').text(),
                    value: value,
                    fieldtype: "checkboxlist"
                };
                if (value.length == 0) {
                    return;
                }
                success(data);
                break;
            case"rate":
                var data = {
                    label: $this.find('.ui-label').text(),
                    value: $this.find('.ui-rate_label').text(),
                    fieldtype: "rate"
                };
                if ($this.find('.stars .active').length == 0) {
                    return;
                }
                success(data);
                break;
            default:
                break;
        }
    };
});